package controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Student;
import pojo.User;
import service.StudentService;
import util.Page;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/list/{currentPage}")
    public String findAll(Student student, @PathVariable("currentPage")int currentPage, Model model){
        Page page=new Page();
        page.setCurrentPage(currentPage);
        int totalCount = studentService.count(student);
        page.setTotalCount(totalCount);
        page.setTotalPage();
        int offset=(currentPage-1)*page.getPageSize();
        List<Student> students=studentService.findAll(student,offset,page.getPageSize());
        model.addAttribute("page",page);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("students",students);
        return "Student/list";
    }

    @RequestMapping("/toadd")
    public String toAdd(){
        return "Student/add";
    }

    @RequestMapping("/addSave")
    public String addStudent(Student student, HttpSession session){
        User user = (User) session.getAttribute("user");
        student.setCreateBy(user.getName());
        student.setStatus("0");
        student.setDelFlag("0");
        int result = studentService.addStudent(student);
        if(result>0){
            return "redirect:/student/list/1";
        }
        else {
            return "error";
        }
    }
    @RequestMapping("/delete/{id}")
    public String deleteStudent(@PathVariable("id") int id){
        int result = studentService.deleteStudent(id);
        if(result>0) {
            return "redirect:/student/list/1";
        }else{
            return "error";
        }
    }

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id,Model model){
        Student student=studentService.findStudentById(id);
        model.addAttribute("student",student);
        return "Student/update";
    }

    @RequestMapping("/updateSave")
    public String updateStudent(Student student,HttpSession session){
        User user = (User) session.getAttribute("user");
        student.setUpdateBy(user.getName());
        int result = studentService.updateStudent(student);
        if(result>0){
            return "redirect:/student/list/1";
        }else{
            return "error";
        }
    }

    @RequestMapping("/markDelete/{id}")
    public String markDelete(@PathVariable("id") int id,HttpSession session){
        Student student=studentService.findStudentById(id);
        User user = (User) session.getAttribute("user");
        student.setUpdateBy(user.getName());
        int result = studentService.markDeleteStudent(student);
        if(result>0){
            return "redirect:/student/list/1";
        }else{
            return "error";
        }
    }
}
